package com.probiz.estore.eai.customer;

import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBElement;

import com.probiz.estore.common.model.eai.log.EaiJobLog;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.eai.common.GenericMarshaller;
import com.probiz.estore.eai.customer.generated.Customer;
import com.probiz.estore.eai.customer.generated.CustomerSet;
import com.probiz.estore.eai.customer.generated.ObjectFactory;
import com.probiz.estore.eai.exception.EaiException;
import com.probiz.estore.eai.log.service.EaiJobLogManager;



public class CustomerMarshaller extends GenericMarshaller<CustomerSet>{	
	CustomerManager customerManager=null;
	EaiJobLogManager eaiJobLogManager=null;

	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}


	@Override
	public JAXBElement<CustomerSet> populateEntitySetElement()
			throws EaiException {
		
		ObjectFactory factory = new ObjectFactory();
		CustomerSet customerSet = factory.createCustomerSet();
		customerSet.setRoottag("customer");
		customerSet.setReceiver("u8");
		
		List<Customer> customerList = customerSet.getCustomer();

		List<com.probiz.estore.common.model.customer.Customer> cList= findExportCustomers();
		if(cList!=null&&cList.size()>0){
			for(com.probiz.estore.common.model.customer.Customer c:cList){
				Customer customer=makeCustomer(factory,c);
				customerList.add(customer);
			}
		}
		
		JAXBElement<CustomerSet> customerDataElement=factory.createUfinterface(customerSet);	
		return customerDataElement;
		
	}
	private Customer makeCustomer(ObjectFactory factory,com.probiz.estore.common.model.customer.Customer c){
		Customer customer = factory.createCustomer();
		customer.setName(c.getUsername());
		customer.setCode(c.getCustomerCode());
		customer.setAbbrname(c.getFullName());
		customer.setEmail(c.getEmail());
		customer.setPhone(c.getTelephone());
		customer.setFax(c.getFax());
		customer.setBankOpen("");
		customer.setDeliverMode("");
		customer.setDevliverSite("");
		customer.setSortCode(c.getMembership()!=null?c.getMembership().getMembershipCode():"999999");
		
		
		return customer;		
	}
   private List<com.probiz.estore.common.model.customer.Customer> findExportCustomers(){
	   List<com.probiz.estore.common.model.customer.Customer> list=null;
	   Date lastExecuteTime=null;
	   String jobName="客户导出";
	   EaiJobLog lastEaiJobLog=this.eaiJobLogManager.findLastEaiJobLogByName(jobName);	   
	   if(lastEaiJobLog!=null){
		   lastExecuteTime=lastEaiJobLog.getJobExecuteTime();
		   if(logger.isDebugEnabled()){
		       logger.debug("客户导出lastExecuteTime:"+lastExecuteTime.toLocaleString());
		   }
	   }
	   list=this.customerManager.findExportCustomer(lastExecuteTime);
	   return list;
   }


public void setEaiJobLogManager(EaiJobLogManager eaiJobLogManager) {
	this.eaiJobLogManager = eaiJobLogManager;
}
	
	
}
